<?php
add_action('init', 'wboxpr_option_rolescheck');
function wboxpr_option_rolescheck() {
    if (current_user_can('edit_theme_options')) {
        add_action('admin_menu', 'wboxpr_option_add_page');
        add_action('admin_init', 'wboxpr_option_init');
        add_action('wp_before_admin_bar_render', 'wboxpr_option_adminbar');
    }
}
add_action('init', 'wboxpr_option_load_sanitization');
function wboxpr_option_load_sanitization() {
    require_once dirname(__FILE__) . '/options-sanitize.php';
}
function wboxpr_option_init() {
    require_once dirname(__FILE__) . '/options-interface.php';
    require_once dirname(__FILE__) . '/options-media-uploader.php';
    $location                = apply_filters('wbox_option_location', array('options.php'));
    $optionsfile             = locate_template($location);
    $wboxpr_option_settings = get_option('wboxpr_option');
    if (function_exists('wboxpr_option_option_name')) {
        wboxpr_option_option_name();
    } elseif (has_action('wboxpr_option_option_name')) {
        do_action('wboxpr_option_option_name');
    } else {
        $default_themename = get_option('stylesheet');
        $default_themename = preg_replace("/\W/", "_", strtolower($default_themename));
        $default_themename = 'wboxpr_option_' . $default_themename;
        if (isset($wboxpr_option_settings['id'])) {
            if ($wboxpr_option_settings['id'] == $default_themename) {
            } else {
                $wboxpr_option_settings['id'] = $default_themename;
                update_option('wboxpr_option', $wboxpr_option_settings);
            }
        } else {
            $wboxpr_option_settings['id'] = $default_themename;
            update_option('wboxpr_option', $wboxpr_option_settings);
        }
    }
    if (!get_option($wboxpr_option_settings['id'])) {
        wboxpr_option_setdefaults();
    }
    register_setting('wboxpr_option', $wboxpr_option_settings['id'], 'wboxpr_option_validate');
    add_filter('option_page_capability_wboxpr_option', 'wboxpr_option_page_capability');
}
function wboxpr_option_page_capability($capability) {
    return 'edit_theme_options';
}
function wboxpr_option_setdefaults() {
    $wboxpr_option_settings = get_option('wboxpr_option');
    $option_name             = $wboxpr_option_settings['id'];
    if (isset($wboxpr_option_settings['knownoptions'])) {
        $knownoptions = $wboxpr_option_settings['knownoptions'];
        if (!in_array($option_name, $knownoptions)) {
            array_push($knownoptions, $option_name);
            $wboxpr_option_settings['knownoptions'] = $knownoptions;
            update_option('wboxpr_option', $wboxpr_option_settings);
        }
    } else {
        $newoptionname                           = array($option_name);
        $wboxpr_option_settings['knownoptions'] = $newoptionname;
        update_option('wboxpr_option', $wboxpr_option_settings);
    }
    $options = &_wboxpr_option_options();
    $values  = wb_get_default_values();
    if (isset($values)) {
        add_option($option_name, $values);}
}

function wboxpr_option_menu_settings() {
    $menu = array(
        'page_title' => 'WBOX PR插件设置 - WBOX西部盒子',
        'menu_title' => 'WBOX PR',
        'capability' => 'edit_theme_options',
        'menu_slug'  => 'wboxpr-options',
        'callback'   => 'wboxpr_option_page',
        'icon_url'   => WBOXPR_ADMIN_DIRECTORY . 'images/wbset.png',
        'position'   => 999,
    );
    return apply_filters('wboxpr_option_menu', $menu);
}
function wboxpr_option_add_page() {
    $menu    = wboxpr_option_menu_settings();
    $wb_page = add_menu_page($menu['page_title'], $menu['menu_title'], $menu['capability'], $menu['menu_slug'], $menu['callback'], $menu['icon_url'], $menu['position']);
    add_action('admin_enqueue_scripts', 'wboxpr_option_load_scripts');
    add_action('admin_print_styles-' . $wb_page, 'wboxpr_option_load_styles');
}
function wboxpr_option_load_styles() {
    wp_enqueue_style('wboxpr_option', WBOXPR_ADMIN_DIRECTORY . 'css/wboxpr_option.css');
    if (!wp_style_is('wp-color-picker', 'registered')) {
        wp_register_style('wp-color-picker', WBOXPR_ADMIN_DIRECTORY . 'css/color-picker.min.css');
    }
    wp_enqueue_style('wp-color-picker');
}
function wboxpr_option_load_scripts($hook) {
    $menu = wboxpr_option_menu_settings();
    if ('toplevel_page_' . $menu['menu_slug'] != $hook) {
        return;
    }

    if (!wp_script_is('wp-color-picker', 'registered')) {
        wp_register_script('iris', WBOXPR_ADMIN_DIRECTORY . 'js/iris.min.js', array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), false, 1);
        wp_register_script('wp-color-picker', WBOXPR_ADMIN_DIRECTORY . 'js/color-picker.min.js', array('jquery', 'iris'));
        $colorpicker_l10n = array(
            'clear'         => __('Clear', 'wbox_option_theme'),
            'defaultString' => __('Default', 'wbox_option_theme'),
            'pick'          => __('Select Color', 'wbox_option_theme'),
        );
        wp_localize_script('wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n);
    }

    wp_enqueue_script('options-custom', WBOXPR_ADMIN_DIRECTORY . 'js/options-custom.js', array('jquery', 'wp-color-picker'));
    add_action('admin_head', 'wb_admin_head');
}
function wb_admin_head() {
    do_action('wboxpr_option_custom_scripts');
}

if (!function_exists('wboxpr_option_page')):
    function wboxpr_option_page() {?>
		<div id="wboxpr_option-wrap" class="wrap">
	    <p class="wboxpr_header"><img class="wbox-logo" src="<?php echo WBOXPR_ADMIN_DIRECTORY; ?>images/wbox.png" /><span>WBOXPR 插件设置</span><a href="https://blog.wbox8.com/" target="_banlk">访问西部盒子博客</a></p>

	    <h2 class="nav-tab-wrapper">
	        <?php echo wboxpr_option_tabs(); ?>
	    </h2>
	    <?php settings_errors('options-framework');?>
	    <div id="wboxpr_option-metabox" class="metabox-holder">
		    <div id="wboxpr_option" class="postbox">
				<form action="options.php" method="post">
				<div id="wboxpr_option-submittop">
					<input type="submit" class="button-primary" name="update" value="<?php esc_attr_e('保存设置', 'wbox_option_theme');?>" />
					<div class="clear"></div>
				</div>
				<?php settings_fields('wboxpr_option');?>
				<?php wboxpr_option_fields();?>
				<div id="wboxpr_option-submit">
					<input type="submit" class="button-primary" name="update" value="<?php esc_attr_e('保存设置', 'wbox_option_theme');?>" /><span>设置面板基于Options Framework开发</span>
					<input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e('恢复默认', 'wbox_option_theme');?>" onclick="return confirm( '<?php print esc_js(__('如果单击“确定”会导致之前所有的设置都丢失，确定要这样做吗？', 'wbox_option_theme'));?>' );" />
					<div class="clear"></div>
				</div>
				</form>
			</div> <!-- / #container -->
		</div>
		<?php do_action('wboxpr_option_after');?>
		</div> <!-- / .wrap -->
	<?php
    }
endif;

function wboxpr_option_validate($input) {
    if (isset($_POST['reset'])) {
        add_settings_error('options-framework', 'restore_defaults', __('Default options restored.', 'wbox_option_theme'), 'updated fade');
        return wb_get_default_values();
    }
    $clean   = array();
    $options = &_wboxpr_option_options();
    foreach ($options as $option) {
        if (!isset($option['id'])) {
            continue;
        }
        if (!isset($option['type'])) {
            continue;
        }
        $id = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($option['id']));
        if ('checkbox' == $option['type'] && !isset($input[$id])) {
            $input[$id] = false;
        }
        if ('multicheck' == $option['type'] && !isset($input[$id])) {
            foreach ($option['options'] as $key => $value) {
                $input[$id][$key] = false;
            }
        }
        if (has_filter('wb_sanitize_' . $option['type'])) {
            $clean[$id] = apply_filters('wb_sanitize_' . $option['type'], $input[$id], $option);
        }
    }
    do_action('wboxpr_option_after_validate', $clean);
    return $clean;
}
function wboxpr_option_save_options_notice() {
    add_settings_error('options-framework', 'save_options', __('设置保存成功！', 'wbox_option_theme'), 'updated fade');
}
add_action('wboxpr_option_after_validate', 'wboxpr_option_save_options_notice');

function wb_get_default_values() {
    $output = array();
    $config = &_wboxpr_option_options();
    foreach ((array) $config as $option) {
        if (!isset($option['id'])) {
            continue;
        }
        if (!isset($option['std'])) {
            continue;
        }
        if (!isset($option['type'])) {
            continue;
        }
        if (has_filter('wb_sanitize_' . $option['type'])) {
            $output[$option['id']] = apply_filters('wb_sanitize_' . $option['type'], $option['std'], $option);
        }
    }
    return $output;
}
function wboxpr_option_adminbar() {
    global $wp_admin_bar;
    $wp_admin_bar->add_menu(array(
        'parent' => 'appearance',
        'id'     => 'wb_theme_options',
        'title'  => '插件设置',
        'href'   => admin_url('themes.php?page=options-framework'),
    ));
}
function &_wboxpr_option_options() {
    static $options = null;
    if (!$options) {
        if (function_exists('wboxpr_option_options')) {
            $options = wboxpr_option_options();
        }
        $options = apply_filters('wb_options', $options);
    }
    return $options;
}
if (!function_exists('wb_option')) {
    function wb_option($name, $default = false) {
        $config = get_option('wboxpr_option');
        if (!isset($config['id'])) {
            return $default;
        }
        $options = get_option($config['id']);
        if (isset($options[$name])) {
            return $options[$name];
        }
        return $default;
    }
}